<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Uploaded_Provider extends Controller
{
    public function action_index($file_id,$suffix = 'default')
    {
        //$this->request->check_cache(sha1($this->request->uri));
    	
        
        $file_model = ORM::factory('file', $file_id);
    
        
    if ($file_model->loaded())
	{
        // Send the file content as the response
            
	    $real_path = $file_model->real_path($suffix);
            
	    $this->response->body(file_get_contents($real_path));
 
        if (!in_array( strtolower($file_model->extension), array('jpg', 'jpeg', 'png', 'gif', 'bmp')))
        {
            $this->response->send_file(TRUE, $file_model->name);
            $file_info = pathinfo($file_model->name.'.'.$file_model->extension);
        }
        else
        {
            $file_info = pathinfo($file_model->id.'.'.$file_model->extension);
        }
	}
	else
	{
            // Return a 404 status
            throw new HTTP_Exception_404();
	}

	// Set the proper headers for allow caching
	$this->response->headers('Content-Type', File::mime_by_ext($file_info['extension'])) ;
	$this->response->headers('Content-Length', (string)filesize($real_path)) ;
	$this->response->headers('Content-Disposition', 'inline; filename="'.$file_info['filename'].'"');
	$this->response->headers('Last-Modified', date('r', filemtime($real_path))); 
    $this->response->headers('Cache-Control', 'max-age=2678400');
        
    }
}